package com.tzf.msst.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tzf.msst.annotation.AuthCheck;
import com.tzf.msst.common.BaseResponse;
import com.tzf.msst.common.ErrorCode;
import com.tzf.msst.common.ResultUtils;
import com.tzf.msst.constant.UserConstant;
import com.tzf.msst.exception.BusinessException;
import com.tzf.msst.exception.ThrowUtils;
import com.tzf.msst.model.dto.userCommentQst.UserCommentQstAddRequest;
import com.tzf.msst.model.dto.userCommentQst.UserCommentQstQueryRequest;
import com.tzf.msst.model.dto.userCommentQst.UserCommentQstUpdateRequest;
import com.tzf.msst.model.entity.User;
import com.tzf.msst.model.entity.UserCommentQst;
import com.tzf.msst.model.vo.UserCommentQstVO;
import com.tzf.msst.service.UserCommentQstService;
import com.tzf.msst.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 用户评论接口
 */
@RestController
@RequestMapping("/userCommentQst")
@Slf4j
public class UserCommentQstController {

    @Resource
    private UserCommentQstService userCommentQstService;

    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 创建用户评论
     *
     * @param userCommentQstAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addUserCommentQst(@RequestBody UserCommentQstAddRequest userCommentQstAddRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userCommentQstAddRequest == null, ErrorCode.PARAMS_ERROR);
        // todo 在此处将实体类和 DTO 进行转换
        UserCommentQst userCommentQst = new UserCommentQst();
        BeanUtils.copyProperties(userCommentQstAddRequest, userCommentQst);
        // 数据校验
        userCommentQstService.validUserCommentQst(userCommentQst, true);
        // todo 填充默认值
        User loginUser = userService.getLoginUser(request);
        userCommentQst.setUserId(loginUser.getId());
        // 写入数据库
        boolean result = userCommentQstService.save(userCommentQst);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        // 返回新写入的数据 id
        long newUserCommentQstId = userCommentQst.getId();
        return ResultUtils.success(newUserCommentQstId);
    }

    /**
     * 删除用户评论
     *
     * @param deleteRequest
     * @param request
     * @return
     */


    /**
     * 更新用户评论（仅管理员可用）
     *
     * @param userCommentQstUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUserCommentQst(@RequestBody UserCommentQstUpdateRequest userCommentQstUpdateRequest) {
        if (userCommentQstUpdateRequest == null || userCommentQstUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        // todo 在此处将实体类和 DTO 进行转换
        UserCommentQst userCommentQst = new UserCommentQst();
        BeanUtils.copyProperties(userCommentQstUpdateRequest, userCommentQst);
        // 数据校验
        userCommentQstService.validUserCommentQst(userCommentQst, false);
        // 判断是否存在
        long id = userCommentQstUpdateRequest.getId();
        UserCommentQst oldUserCommentQst = userCommentQstService.getById(id);
        ThrowUtils.throwIf(oldUserCommentQst == null, ErrorCode.NOT_FOUND_ERROR);
        // 操作数据库
        boolean result = userCommentQstService.updateById(userCommentQst);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 根据 id 获取用户评论（封装类）
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<UserCommentQstVO> getUserCommentQstVOById(long id, HttpServletRequest request) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        UserCommentQst userCommentQst = userCommentQstService.getById(id);
        ThrowUtils.throwIf(userCommentQst == null, ErrorCode.NOT_FOUND_ERROR);
        // 获取封装类
        return ResultUtils.success(userCommentQstService.getUserCommentQstVO(userCommentQst, request));
    }

    /**
     * 分页获取用户评论列表（仅管理员可用）
     *
     * @param userCommentQstQueryRequest
     * @return
     */
    @PostMapping("/list/page")
    @SaCheckRole(UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserCommentQst>> listUserCommentQstByPage(@RequestBody UserCommentQstQueryRequest userCommentQstQueryRequest) {
        long current = userCommentQstQueryRequest.getCurrent();
        long size = userCommentQstQueryRequest.getPageSize();
        // 查询数据库
        Page<UserCommentQst> userCommentQstPage = userCommentQstService.page(new Page<>(current, size),
                userCommentQstService.getQueryWrapper(userCommentQstQueryRequest));
        return ResultUtils.success(userCommentQstPage);
    }

    /**
     * 分页获取用户评论列表（封装类）
     *
     * @param userCommentQstQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<UserCommentQstVO>> listUserCommentQstVOByPage(@RequestBody UserCommentQstQueryRequest userCommentQstQueryRequest,
                                                               HttpServletRequest request) {
        long current = userCommentQstQueryRequest.getCurrent();
        long size = userCommentQstQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Page<UserCommentQst> userCommentQstPage = userCommentQstService.page(new Page<>(current, size),
                userCommentQstService.getQueryWrapper(userCommentQstQueryRequest));
        // 获取封装类
        return ResultUtils.success(userCommentQstService.getUserCommentQstVOPage(userCommentQstPage, request));
    }

    /**
     * 分页获取当前登录用户创建的用户评论列表
     *
     * @param userCommentQstQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/my/list/page/vo")
    public BaseResponse<Page<UserCommentQstVO>> listMyUserCommentQstVOByPage(@RequestBody UserCommentQstQueryRequest userCommentQstQueryRequest,
                                                                 HttpServletRequest request) {
        ThrowUtils.throwIf(userCommentQstQueryRequest == null, ErrorCode.PARAMS_ERROR);
        // 补充查询条件，只查询当前登录用户的数据
        User loginUser = userService.getLoginUser(request);
        userCommentQstQueryRequest.setUserId(loginUser.getId());
        long current = userCommentQstQueryRequest.getCurrent();
        long size = userCommentQstQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        // 查询数据库
        Page<UserCommentQst> userCommentQstPage = userCommentQstService.page(new Page<>(current, size),
                userCommentQstService.getQueryWrapper(userCommentQstQueryRequest));
        // 获取封装类
        return ResultUtils.success(userCommentQstService.getUserCommentQstVOPage(userCommentQstPage, request));
    }

    // endregion
}
